package com.stylefeng.guns.modular.owBackstageMgr.newsCategoryMgr.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.core.util.UserAgentUtil;
import com.stylefeng.guns.modular.owBackstageMgr.newsCategoryMgr.service.INewsCategoryService;
import com.stylefeng.guns.modular.system.dao.NewsCategoryMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.model.NewsCategory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章类别表 服务实现类
 * </p>
 *
 * @author huangyuehong123
 * @since 2018-06-20
 */
@Service
public class NewsCategoryServiceImpl extends ServiceImpl<NewsCategoryMapper, NewsCategory> implements INewsCategoryService {

    /**
     * 获取所有的新闻类别
     */
    @Override
    public List<Map<String, Object>> getNewsCategories(Page<NewsCategory> page, String name, String orderByField, boolean asc) {
        return this.baseMapper.getNewsCategories(page,name,orderByField,asc);
    }

    /**
     * 根据themeId主题id获取它所有的类别对象
     */
    @Override
    public List<NewsCategory> getCategoriesByThemeId(Long themeId, HttpServletRequest request) {
        //return this.baseMapper.getCategoriesByThemeId(themeId);
        EntityWrapper<NewsCategory> wrapper = new EntityWrapper<>();
        wrapper.eq("theme_id",themeId);
        wrapper.eq("is_del",0);
        wrapper.orderBy("order_value",true);

        List<NewsCategory> categories = selectList(wrapper);

        for (NewsCategory category : categories){
            // 判断是PC端还是移动端发送的请求
            if (UserAgentUtil.isMobile(request)){
                if (ToolUtil.isNotEmpty(category.getWapRequestUrl())){
                    category.setRequestUrl(category.getWapRequestUrl());
                }
            }
        }

        return categories;
    }

    /**
     * 用类型名称获取类型id
     * @param name 类型名称
     * @return
     */
    @Override
    public Long getIdByName(String name) {
        EntityWrapper<NewsCategory> wrapper = new EntityWrapper<>();
        wrapper.eq("name",name);
        wrapper.eq("is_del",0);

        NewsCategory category = selectOne(wrapper);
        if (category != null && category.getId() != null){
            return category.getId();
        }
        return null;
    }


}
